import { CARD_POSITION } from '@/framework/types/portal.d'
import { addPropertyValueToObject, getAssetsFile } from '@/framework/utils/common'
import { useDictStore } from '@/framework/store/common'
import { CommonStringObjectType, LabelContentArray } from '@/framework/types/common-data'

export type CoverPropertyListType =
  | {
      property: string
      isInline: boolean
    }
  | string

const dict = useDictStore()

export const getPropertyNameByKey = (key: string, columnsArrayMap: any) => columnsArrayMap[key].displayName

export const getCardTitle = (data: any, nameColumn: string, showPositionMap: any) => {
  const titleProperty = showPositionMap[CARD_POSITION.TITLE]
  if (!titleProperty) return data[nameColumn]
  else return data[titleProperty[0]]
}

export const getCardSubTitle = (data: any, showPositionMap: any) => {
  const subTitleProperty = showPositionMap[CARD_POSITION.SUB_TITLE]
  if (subTitleProperty) return data[subTitleProperty[0]]
  return ''
}

export const getCardTags = (data: any, showPositionMap: any) => {
  const tags = showPositionMap[CARD_POSITION.TAG]
  if (!tags) return []
  return tags.map((tag: any) => data[tag]).filter(Boolean)
}

export const getCardBadge = (
  data: any,
  showPositionMap: any,
  columnsArrayMap: any,
  columnsReferenceDictMap: any
) => {
  const badge = showPositionMap[CARD_POSITION.BADGE]
  if (!badge) return undefined
  const property = badge[0]
  const text = data[property] as string
  const { entityField } = columnsArrayMap[property]
  let color = ''
  if (entityField) {
    const colorList = entityField.split(',')
    const referenceDict = columnsReferenceDictMap[property]
    const index = dict.getIndex(referenceDict, 'label', text)
    color = colorList[index]
  }
  return { text, color }
}

export const getCardLeftBottomText = (data: any, showPositionMap: any, allStaffMap: any, columnsArrayMap: any) => {
  const leftBottomText = showPositionMap[CARD_POSITION.OWNER]
  if (!leftBottomText) return ''
  const staffNumber = data[leftBottomText[0]]
  return allStaffMap[staffNumber]?.label || '暂无' + getPropertyNameByKey(leftBottomText[0], columnsArrayMap)
}

export const getCardLeftBottomImg = (data: any, showPositionMap: any, allStaffMap: any) => {
  const leftBottomImg = showPositionMap[CARD_POSITION.OWNER]
  if (!leftBottomImg) return ''
  const staffNumber = data[leftBottomImg[0]]
  return allStaffMap[staffNumber]?.pictureLink || getAssetsFile('default/avatar.png')
}

export const getCardRightBottomText = (data: any, showPositionMap: any, columnsArrayMap: any) => {
  const rightBottomText = showPositionMap[CARD_POSITION.TIME]
  if (!rightBottomText) return ''
  const text = rightBottomText[0]
  return `${getPropertyNameByKey(text, columnsArrayMap)}：${data[text]}`
}

export const getContentItemList = (
  data: any,
  showPositionMap: any,
  columnsArrayMap: any,
  coverPropertyList: Array<any>
) => {
  const defaultInlineValue = true
  if (coverPropertyList.length)
    return coverPropertyList.map((property: CoverPropertyListType) => {
      if (typeof property === 'string') {
        return {
          label: getPropertyNameByKey(property, columnsArrayMap),
          content: data[property],
          inline: defaultInlineValue
        }
      } else {
        return {
          label: getPropertyNameByKey(property.property, columnsArrayMap),
          content: data[property.property],
          inline: property.isInline
        }
      }
    })
  const cardContentList = showPositionMap[CARD_POSITION.CONTENT]
  const cardLabelList = showPositionMap[CARD_POSITION.LABEL]
  let contentList = [] as Array<any>
  if (cardContentList) {
    contentList = cardContentList.map((property: string) => {
      return {
        label: getPropertyNameByKey(property, columnsArrayMap),
        content: data[property],
        inline: false
      }
    })
  }
  let labelList = [] as Array<any>
  if (cardLabelList) {
    labelList = cardLabelList.map((property: string) => {
      return {
        label: getPropertyNameByKey(property, columnsArrayMap),
        content: data[property],
        inline: true
      }
    })
  }
  return labelList.concat(contentList)
}

export const onclickCard = (
  data: CommonStringObjectType,
  columnsArrayMap: any,
  detailDataList: any,
  showDetail: Ref<boolean>
) => {
  const detailDataObj = {} as { [key: string]: LabelContentArray }
  Object.entries(data)
    .filter(
      ([key]: any) =>
        columnsArrayMap[key] && columnsArrayMap[key].enable === '1' && columnsArrayMap[key].detailShow === '1'
    )
    .forEach(([key, value]) => {
      const displayGroupName = String(columnsArrayMap[key].displayGroupName || '')
      const displayOrder = columnsArrayMap[key].displayOrder
      const result = {
        label: getPropertyNameByKey(key, columnsArrayMap),
        content: value || '暂无数据',
        displayOrder
      }
      addPropertyValueToObject(detailDataObj, displayGroupName, result)
    })
  const dataList = Object.entries(detailDataObj)
    .map(([key, value]: any) => {
      return {
        title: key,
        dataList: value.sort((a: any, b: any) => a.displayOrder - b.displayOrder)
      }
    })
    .sort((a: any, b: any) => a.title.localeCompare(b.title))
  if (dataList.length && !dataList[0].title) {
    dataList.push(dataList[0])
    dataList.splice(0, 1)
  }
  detailDataList.value = dataList
  showDetail.value = true
}
